import inputCommon from '../../../../mixin/inputCommon'
// import dialogTemplate from './dialogTemplate'
import _ from 'lodash'

export default {
  // components: { dialogTemplate },
  name: 'buttonRender',
  props: ['config', 'editor', 'scope'],
  inject: ['rootForm'],
  mixins: [inputCommon],
  data() {
    return {
      visible: false,
      data: undefined, // 弹窗初始值
      dialogList: [], // 弹窗子页面
      dialogLoading: false,
      dialogProperties: {}, // 弹窗表单
      throttleConfirm: _.throttle(() => {
        this.confirm()
      }, 300)
    }
  },
  methods: {
    before() {
      if (this.config.beforeHook) {
        return this.getParsedFn('beforeHook')()
      }
      return Promise.resolve()
    },
    handler() {
      switch (this.config.type) {
        case 'custom':
          return this.getParsedFn('handler')()
        case 'link':
          let href = this.getParsedEx('linkExpression')()
          window.open(href, '_blank')
          return
        case 'observerTrigger':
          return this.rootForm.dataInterfaceObserverManage.get(this.config.dataInterfaceId).update(this.getParsedEx('interfaceParams')() || {}, this.getParsedEx('dataInterfaceRequestBody')() || undefined)
        case 'routerLink':
          let routerQuery = this.getParsedEx('routerQuery')()
          let query = routerQuery || {}
          query.pageId = this.config.routerPage
          query.applicationId = this.$route.query.applicationId
          return new Function('$router', '$lightAppJssdk', 'return ' + process.env.VUE_APP_ROUTER_LINK_FN)(this.$router, window.lightAppJssdk)(query, this.config.routerLinkTitle, process.env.VUE_APP_PUBLIC_PATH)
        // this.$router.push({ path: '/viewPage', query: query })
        case 'dialog':
          this.visible = true
          // this.data = this.getParsedEx('dialogInitialData')()
          if (this.config.dialogInitialData) {
            this.dialogLoading = true
            Promise.resolve(this.getParsedFn('dialogInitialData')()).then(data => {
              this.data = data
              _.assign(this.dialogProperties, this.data)
              this.dialogLoading = false
            }).finally(() => {
              this.dialogLoading = false
            })
          }
          return
        case 'printing':
          window.print()
          return
      }
    },
    after() {
      if (this.config.afterHook) {
        this.getParsedFn('afterHook')()
      }
    },
    do: async function () {
      if(this.config.hasLoading) {
        this.rootForm.loading = true
      }
      await this.before()
      await this.handler()
      await this.after()
    },
    confirm() {
      if (!this.editor) {
        if (this.config.needConfirm) {
          this.$confirm(this.getParsedEx('confirmContent')(), this.getParsedEx('confirmTitle')(), {
            confirmButtonText: '确定',
            cancelButtonText: '取消',
            type: 'warning'
          }).then(() => {
            this.do()
          }).catch(() => {

          })
        } else {
          this.do()
        }
      }
    },
    dialogCancel() {
      this.visible = false
      this.data = undefined
      this.dialogProperties = {}
    },
    dialogConfirm(valid) {
      if(this.config.hasLoading) {
        this.rootForm.loading = true
      }
      switch (this.config.dialogBehavior) {
        case 'request':
          if (valid) {
            let dialogdataInterfaceRequestBody = {}
            if (!this.config.dialogdataInterfaceRequestBody) {
              dialogdataInterfaceRequestBody = Object.assign(this.data || {}, this.dialogProperties)
            } else {
              dialogdataInterfaceRequestBody = this.getParsedEx('dialogdataInterfaceRequestBody')()
            }
            this.rootForm.dataInterfaceObserverManage.get(this.config.dialogdataInterfaceId).update(this.getParsedEx('dialogdataInterfaceParams')() || {}, dialogdataInterfaceRequestBody).then(one => {
              this.dialogCancel()
              if (this.config.dialogAfterSuccessHook) {
                this.getParsedFn('dialogAfterSuccessHook')(_.cloneDeep(this.dialogProperties))
              }
            })
          }
          break
        case 'custom':
          if (valid) {
            this.getParsedFn('dialogCustomCb')(_.cloneDeep(this.dialogProperties))
            // alert(JSON.stringify(this.dialogProperties))
            this.dialogCancel()
            if (this.config.dialogAfterSuccessHook) {
              this.getParsedFn('dialogAfterSuccessHook')(_.cloneDeep(this.dialogProperties))
            }
          }
          break
      }
      this.rootForm.loading = false
    }
  },
  // beforeDestroy() {
  //   if (this.subscribe) {
  //     this.rootForm.pluginModelObserverManage.get(this.config.name).unsubscribe(this.subscribe)
  //   }
  // },
  created() {
    if (this.config.selectDialog && this.config.type === 'dialog') {
      // this.dialogList = (_.find(this.config.selectDialog.sortable, one => one.name === this.config.selectDialog.name) && _.find(this.config.selectDialog.sortable, one => one.name === this.config.selectDialog.name).list) || []
      this.dialogList = (_.find(this.rootForm.sortable, one => one.name === this.config.selectDialog) && _.find(this.rootForm.sortable, one => one.name === this.config.selectDialog).list) || []
      // this.dialogProperties = _.find(this.config.selectDialog.sortable, one => one.name === this.config.selectDialog.name).properties || {}
    }

    // this.subscribe = data => {
    //   ['title', 'visible', 'data'].forEach(one => {
    //     if (one in data) {
    //       this[one] = data[one]
    //     }
    //   })
    // }
    // this.rootForm.pluginModelObserverManage.get(this.config.name).subscribe(this.subscribe)
  }

}
